package example.springboot.controller;

import example.springboot.dto.PaginationDTO;
import example.springboot.mapper.questionListMapper;
import example.springboot.mapper.questionMapper;
import example.springboot.mapper.userMapper;
import example.springboot.model.Question;
import example.springboot.model.t_user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 我的提问
 */
@Controller
public class ProfileController {
    @Autowired
    Question question;
    @Autowired
    example.springboot.mapper.questionMapper questionMapper;
    @Autowired
    questionListMapper questionListMapper;
    @RequestMapping("/questions")
    public String myQuestionDisplay(HttpServletRequest request,
                                    Model model,
                                    @RequestParam(name = "page", defaultValue = "1") Integer page){
        t_user t_user = (example.springboot.model.t_user) request.getSession().getAttribute("t_user");
        if (t_user!=null){
            List<Question> questions = questionMapper.byCreatorFind(t_user.getAccountid());
            Integer limit;
            PaginationDTO<Question> paginationDTO=new PaginationDTO();
            paginationDTO.setData(questions);
            limit=questions.size()/5;
            if (questions.size()%5!=0) limit++;
            paginationDTO.setTotalPage(limit);//设置页数
            paginationDTO.setPagination(limit,page);
            //获取每页的问题
            List<Question> listquestion= questionListMapper.questionlist((paginationDTO.getPage()-1)*5,5);
            question.setUser(t_user);
            paginationDTO.setData(listquestion);
            model.addAttribute("pagination",paginationDTO);
            return "profile";
        }else {
            return "/";
        }
    }

}
